import store from '@/store'

function checkPermission (el, binding) {
  const { value } = binding
  const permissionCodeList = store.getters.permissionCodeList
  const valueArr = Array.isArray(value) ? value : [value]
  if (value && valueArr.length > 0) {
    const hasPermission = valueArr.some(code => {
      return permissionCodeList.includes(code)
    })
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error('请设置操作权限')
  }
}

export default {
  // 在绑定元素的父组件被挂载后调用
  mounted (el, binding) {
    checkPermission(el, binding)
  },
  // 在包含组件的 VNode 及其子组件的 VNode 更新后调用
  update (el, binding) {
    checkPermission(el, binding)
  }
}
